import { Component, OnInit, EventEmitter } from '@angular/core';
import { TableParams, QuoteHeaders, QuoteHeader } from "./../quote-table/quote-table.component";
import { ReplaySubject } from "rxjs/ReplaySubject";
import { Subject } from "rxjs/Subject";
import { Observable } from "rxjs/Observable";



interface Brief {
  name: string;
  label: string;
  headers: QuoteHeaders,
  checked?: boolean;
  dataUrl: string;
}

interface BriefTable extends Brief {
  source: Observable<TableParams>;
}

@Component({
  selector: 'quote-overview',
  templateUrl: 'quote-overview.component.html',
  styleUrls: [
    'quote-overview.component.less'
  ]
})
export class QuoteOverviewComponent implements OnInit {
  briefTableList: BriefTable[];
  briefList: Brief[] = require("./briefs.json");
  briefToggle = new Subject<Brief[]>();
  stockSource = new EventEmitter();

  szSub = new ReplaySubject(1);
  scSub = new ReplaySubject(1);
  cySub = new ReplaySubject(1);

  constructor() { }

  ngOnInit() {
    let { briefToggle, briefList } = this;
    // briefToggle.debounceTime(1000).subscribe(briefList =>
    briefToggle.subscribe(briefList =>
      this.briefTableList = briefList
        .filter(brief => brief.checked)
        .map(brief =>
          Object.assign({}, brief, {
            source: Observable.from([{
              dataUrl: brief.dataUrl
            }])
          })
        )
    );

    briefToggle.next(briefList);
    // 展示分时
    this.szSub.next({
      symbol: "000001.SS"
    });
    this.scSub.next({
      symbol: "399001.SZ"
    });
    this.cySub.next({
      symbol: "399006.SZ"
    });
  }

  refreshBriefList() {
    console.debug("Refresh: ", this.briefList);
    this.briefToggle.next(this.briefList);
  }

  setCurrentStock(stock) {
    this.stockSource.next(stock);
  }

}